package com.googlecode.cpbi.util;

import java.util.HashSet;
import java.util.Set;

import org.springframework.util.StringUtils;
import org.vafer.dependency.asm.DependencyVisitor;

/**
 * @author Jeremy Wales
 */
public class PackageCollectingDependencyVisitor extends DependencyVisitor {
	private final String[] excludedPackagePatterns;
	private final Set packageDependencies = new HashSet();

	public PackageCollectingDependencyVisitor(String[] excludedPackagePatterns) {
		this.excludedPackagePatterns = excludedPackagePatterns;
	}

	protected String visitDependency(String className) {
		String packageName = PackageUtils.getPackageNameOf(className);
		if (StringUtils.hasText(packageName) && !PackageUtils.matches(packageName, excludedPackagePatterns))
			packageDependencies.add(packageName);
		return className;
	}

	public Set getPackageDependencies() {
		return packageDependencies;
	}
}
